import { request } from "../../request/index.js";
import regeneratorRuntime from "../../lib/runtime/runtime";
Page({
  data: {
    tabs: [
      {
        id: 0,
        value: "综合",
        isActive: true,
      },
      {
        id: 1,
        value: "销量",
        isActive: false,
      },
      {
        id: 2,
        value: "价格",
        isActive: false,
      },
    ],
    goodsList: [],
  },

  QueryParams: {
    query: "",
    cid: "",
    pagenum: 1,
    pagesize: 10,
  },

  totalPages: 0,
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(this.QueryParams.query)
    this.QueryParams.cid = options.cid || "";
    this.QueryParams.query = options.query || "";
    this.getGoodsList();
  },

  async getGoodsList() {
    const res = await request({
      url: "/goods/search",
      data: this.QueryParams,
    });

    const total = res.total;
    this.totalPages = Math.ceil(total / this.QueryParams.pagesize);
    this.setData({
      goodsList: [...this.data.goodsList, ...res.goods],
    });

    // 关闭下拉刷新的窗口
    wx.stopPullDownRefresh();
  },

  handleTabsItemChange(e) {
    const { index } = e.detail;
    let { tabs } = this.data;
    tabs.forEach((v, i) =>
      i === index ? (v.isActive = true) : (v.isActive = false)
    );
    this.setData({
      tabs,
    });
  },
  // 上拉刷新
  onReachBottom() {
    if (this.QueryParams.pagenum >= this.totalPages) {
      wx.showToast({
        title: "没有下一页了",
      });
    } else {
      this.QueryParams.pagenum++;
      this.getGoodsList();
    }
  },
  //下拉刷新
  onPullDownRefresh() {
    // 充值数组
    this.setData({
      goodsList: []
    })
    //重置页码
    this.QueryParams.pagenum = 1;
    // 请求数据
    this.getGoodsList();
  }
});
